/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package parsexml;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XML11Serializer;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author VANANH
 */
public class DemoDOM {
    public static void main(String[] args) {
        
        try {
            URL url = new URL("http://tapchilaptrinh.vn/2014/06/10/xu-huong-ngon-ngu-lap-trinh-nam-2014/");
            URLConnection conn = url.openConnection();
            conn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
            Source s  = new Source(conn.getInputStream());
//            List<net.htmlparser.jericho.Element> titles = s.getAllElements(HTMLElementName.TITLE);
            net.htmlparser.jericho.Element e = s.getAllElementsByClass("entry-content clearfix").get(0);
            System.out.println(e.getAllElements());
            
//        try {
//            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//            DocumentBuilder builder = factory.newDocumentBuilder();
//            Document document = builder.parse("data.xml");
//            
//            NodeList list = document.getElementsByTagName("Name");
//            
//            for (int i = 0; i < list.getLength(); i++) {
//                Node node = list.item(i);
//                System.out.println("Title : " + node.getTextContent());
//                
//            }
//            
//            NodeList lst = document.getElementsByTagName("Person");
//            for (int i = 0; i < lst.getLength(); i++) {
//                Node node = lst.item(i);
//                NamedNodeMap map = node.getAttributes();
//                System.out.println(map.getNamedItem("id").getNodeValue());
//            }
//
//            
//            Node root = document.getElementsByTagName("root").item(0);
//            Node p = document.createElement("Person");
//            Attr id = document.createAttribute("id");
//           
//            ((Element)p).setAttributeNode(id);
//            
//            Node name = document.createElement("Name");
//            name.setTextContent("Nguyen A");
//            p.appendChild(name);
//            root.appendChild(p);
//            OutputFormat format = new OutputFormat(document);
//            format.setLineWidth(65);
//            format.setIndenting(true);
//            format.setIndent(2);
//            
//            XMLSerializer serial = new XMLSerializer(new FileOutputStream("newdata.xml"),format);
//            serial.serialize(document);
//            
//            treeWalker(document);
//
//            
//        } catch (Exception ex) {
//            Logger.getLogger(DemoDOM.class.getName()).log(Level.SEVERE, null, ex);
//        }
        } catch (Exception ex) {
            Logger.getLogger(DemoDOM.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private static void treeWalker(Node currentNode){
        NodeList childs = currentNode.getChildNodes();
        for (int i = 0; i < childs.getLength(); i++) {
            Node c = childs.item(i);
            System.out.println(c.getNodeName());
            treeWalker(c);
        }
    }
}
